package com.jvup.common;

import android.app.Activity;
import android.content.Context;

import androidx.multidex.MultiDexApplication;

import com.jvup.common.model.entity.UserEntity;
import com.jvup.common.utils.CacheStorageUtil;
import com.jvup.common.utils.EquipmentInfoUtil;
import com.jvup.common.utils.OkGoConfigHelper;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.lifecycleHandler.ActivityLifecycleHandler;

//import com.scwang.smartrefresh.layout.SmartRefreshLayout;
//import com.scwang.smartrefresh.layout.api.RefreshLayout;
//import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
//import com.scwang.smartrefresh.layout.header.ClassicsHeader;

public class SummerApp extends MultiDexApplication {

    private static SummerApp summerApp;

//    private NetWorkChangReceiver netWorkChangReceiver;

//    //static 代码段可以防止内存泄露
//    static {
//        //设置全局的Header构建器
//        SmartRefreshLayout.setDefaultRefreshHeaderCreator((Context context, RefreshLayout layout)->{
//            //全局设置主题颜色
////            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//            //指定为经典Header，默认是 贝塞尔雷达Header
//            return new ClassicsHeader(context);
//            //return new ClassicsHeader(context).setTimeFormat(new DynamicTimeFormat("更新于 %s"));
//        });
//        //设置全局的Footer构建器
//        SmartRefreshLayout.setDefaultRefreshFooterCreator((Context context, RefreshLayout layout)->{
//            //指定为经典Footer，默认是 BallPulseFooter
//            return new ClassicsFooter(context).setDrawableSize(20);
//        });
//    }

    public static SummerApp summerApp() {
        return summerApp;
    }

    public static Context getContext() {
        return summerApp;
    }

    @Override
    public void onCreate() {
        AntsRouter.init(this);
        super.onCreate();
        summerApp = this;

        // 初始化缓存工具，大部分需要行为需要依赖缓存，固应该优先执行初始化
        CacheStorageUtil.init(this);


//        //定时任务队列初始化
//        TimeTaskQueue.QUEUE.init();
//
        //用户数据本地加载缓存
        UserEntity.self.loadLocalUserInfo();
        //屏幕像素度量单位初始化
        EquipmentInfoUtil.init(this);

        // 初始化网络请求头，这个头还在webview中加载页面用了
        OkGoConfigHelper.initHeader(this);
        // okGo网络框架初始化
        OkGoConfigHelper.initOkGo(this);

//        // MobSDK 分享
//        MobSDK.init(this);
//
//        // 全局配置初始化
////        GlobalConfig.init();
//        // 友盟统计
//        UMAnalytics.init(this);
        // 应用前后台切换监听
        registerActivityLifecycleCallbacks(new AppFrontBackStateListening());
    }

    /**
     * 移除网络监听
     */
    private void removeNetworkListener() {
//        if(netWorkChangReceiver != null)
//            netWorkChangReceiver.unregisterReceiver();
    }

    /**
     * 添加网络监听，网络切换进行提示
     */
    private void addNetworkListener() {
//        netWorkChangReceiver = new NetWorkChangReceiver(this);
//        netWorkChangReceiver.registerReceiver();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
//        if(level == TRIM_MEMORY_UI_HIDDEN){
//            Glide.get(this).clearMemory();
//        }
//        Glide.get(this).trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
//        Glide.get(this).onLowMemory();
    }

    /**
     * Activity 生命周期监听，用于监控app前后台状态切换
     */
    public class AppFrontBackStateListening extends ActivityLifecycleHandler {
        /**
         * 当前Activity个数
         **/
        private int activityNumber = 0;

        public AppFrontBackStateListening(){}

        private void onEnteredAppForeground() {
            // App just entered the foreground. Do something here!
            addNetworkListener();
        }
        private void onEnteredAppBackground() {
            // App just entered the background. Do something here!
            removeNetworkListener();
        }

        @Override
        public void onActivityStarted(Activity activity) {
            if (activityNumber == 0) {
                // app回到前台
                onEnteredAppForeground();
            }
            activityNumber++;
        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityNumber--;
            if (activityNumber == 0) {
                // app回到后台
                onEnteredAppBackground();
            }
        }
    }

}